<!DOCTYPE html>
<html>
<head>
    <title>Charge Distribution</title>
</head>
<body>
<canvas id="physicsCanvas" width="800" height="420"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // --- Style and General Settings ---
    const fontSize = 20;
    const fontFamily = 'Times New Roman';
    const dotRadius = 4;
    ctx.fillStyle = 'black';
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1.5;

    // --- Coordinates ---
    const y1 = 80;
    const y2 = 180;
    const y3 = 280;
    const figY = 370;

    const xLabel = 100;
    const lineStartX = 150;
    const lineEndX = 750;
    
    const xCenter = 380;
    const aHalf = 150;
    const xMinusAHalf = xCenter - aHalf; // 230
    const xPlusAHalf = xCenter + aHalf;  // 530
    const xObs = 680;

    // --- Helper Functions ---
    function drawDot(x, y) {
        ctx.beginPath();
        ctx.arc(x, y, dotRadius, 0, 2 * Math.PI);
        ctx.fill();
    }

    function drawLine(y) {
        ctx.beginPath();
        ctx.moveTo(lineStartX, y);
        ctx.lineTo(lineEndX, y);
        ctx.stroke();
    }

    // Draws a fraction like a/2. Returns its approximate width.
    function drawFraction(x, y, num, den) {
        ctx.save();
        ctx.textAlign = 'center';
        
        ctx.font = `italic ${fontSize - 2}px ${fontFamily}`;
        const numMetrics = ctx.measureText(num);
        ctx.fillText(num, x, y - 5);
        
        ctx.font = `normal ${fontSize - 2}px ${fontFamily}`; // Denominator is a number
        const denMetrics = ctx.measureText(den);
        ctx.fillText(den, x, y + 13);
        
        const lineWidth = Math.max(numMetrics.width, denMetrics.width) + 6;
        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.moveTo(x - lineWidth / 2, y + 2);
        ctx.lineTo(x + lineWidth / 2, y + 2);
        ctx.stroke();
        ctx.restore();
        return lineWidth;
    }
    
    // --- Draw Part (I) ---
    ctx.font = `normal ${fontSize}px ${fontFamily}`;
    ctx.textAlign = 'left';
    ctx.fillText("(I)", xLabel, y1 + 5);
    drawLine(y1);
    drawDot(xCenter, y1);
    drawDot(xObs, y1);
    
    ctx.textAlign = 'center';
    ctx.font = `italic ${fontSize}px ${fontFamily}`;
    ctx.fillText("q", xCenter, y1 - 15);
    ctx.fillText("x", xObs, y1 + 25);
    // Draw label "x' = 0" centered under the point
    {
        const yLabel = y1 + 25;
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        const w1 = ctx.measureText("x'").width;
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        const w2 = ctx.measureText(" = 0").width;
        const totalW = w1 + w2;
        const startX = xCenter - totalW / 2;
        ctx.textAlign = 'left';
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        ctx.fillText("x'", startX, yLabel);
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        ctx.fillText(" = 0", startX + w1, yLabel);
    }
    

    // --- Draw Part (II) ---
    ctx.font = `normal ${fontSize}px ${fontFamily}`;
    ctx.textAlign = 'left';
    ctx.fillText("(II)", xLabel, y2 + 5);
    drawLine(y2);
    drawDot(xMinusAHalf, y2);
    drawDot(xPlusAHalf, y2);
    drawDot(xObs, y2);

    ctx.textAlign = 'center';
    ctx.font = `italic ${fontSize}px ${fontFamily}`;
    ctx.fillText("-q", xMinusAHalf, y2 - 15);
    ctx.fillText("+q", xPlusAHalf, y2 - 15);
    ctx.fillText("x", xObs, y2 + 25);
    
    const yLabel2 = y2 + 25;
    // Position label: x' = -a/2
    {
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        const w1 = ctx.measureText("x'").width;
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        const w2 = ctx.measureText(" = -").width;
        const w3 = 25; // Approx width of fraction
        const totalW = w1 + w2 + w3;
        const startX = xMinusAHalf - totalW / 2;
        ctx.textAlign = 'left';
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        ctx.fillText("x'", startX, yLabel2);
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        ctx.fillText(" = -", startX + w1, yLabel2);
        drawFraction(startX + w1 + w2 + w3 / 2, yLabel2 - 7, 'a', '2');
    }

    // Position label: x' = a/2
    {
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        const w1 = ctx.measureText("x'").width;
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        const w2 = ctx.measureText(" = ").width;
        const w3 = 25; // Approx width of fraction
        const totalW = w1 + w2 + w3;
        const startX = xPlusAHalf - totalW / 2;
        ctx.textAlign = 'left';
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        ctx.fillText("x'", startX, yLabel2);
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        ctx.fillText(" = ", startX + w1, yLabel2);
        drawFraction(startX + w1 + w2 + w3 / 2, yLabel2 - 7, 'a', '2');
    }

    // --- Draw Part (III) ---
    ctx.font = `normal ${fontSize}px ${fontFamily}`;
    ctx.textAlign = 'left';
    ctx.fillText("(III)", xLabel, y3 + 5);
    drawLine(y3);
    drawDot(xMinusAHalf, y3);
    drawDot(xCenter, y3);
    drawDot(xPlusAHalf, y3);
    drawDot(xObs, y3);
    
    ctx.textAlign = 'center';
    ctx.font = `italic ${fontSize}px ${fontFamily}`;
    ctx.fillText("q", xMinusAHalf, y3 - 15);
    ctx.fillText("-2q", xCenter, y3 - 15);
    ctx.fillText("q", xPlusAHalf, y3 - 15);
    ctx.fillText("x", xObs, y3 + 25);
    
    const yLabel3 = y3 + 25;
    // Position label: x' = -a/2
    {
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        const w1 = ctx.measureText("x'").width;
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        const w2 = ctx.measureText(" = -").width;
        const w3 = 25;
        const totalW = w1 + w2 + w3;
        const startX = xMinusAHalf - totalW / 2;
        ctx.textAlign = 'left';
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        ctx.fillText("x'", startX, yLabel3);
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        ctx.fillText(" = -", startX + w1, yLabel3);
        drawFraction(startX + w1 + w2 + w3 / 2, yLabel3 - 7, 'a', '2');
    }

    // Position label: x' = 0
    {
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        const w1 = ctx.measureText("x'").width;
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        const w2 = ctx.measureText(" = 0").width;
        const totalW = w1 + w2;
        const startX = xCenter - totalW / 2;
        ctx.textAlign = 'left';
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        ctx.fillText("x'", startX, yLabel3);
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        ctx.fillText(" = 0", startX + w1, yLabel3);
    }

    // Position label: x' = a/2
    {
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        const w1 = ctx.measureText("x'").width;
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        const w2 = ctx.measureText(" = ").width;
        const w3 = 25;
        const totalW = w1 + w2 + w3;
        const startX = xPlusAHalf - totalW / 2;
        ctx.textAlign = 'left';
        ctx.font = `italic ${fontSize}px ${fontFamily}`;
        ctx.fillText("x'", startX, yLabel3);
        ctx.font = `normal ${fontSize}px ${fontFamily}`;
        ctx.fillText(" = ", startX + w1, yLabel3);
        drawFraction(startX + w1 + w2 + w3 / 2, yLabel3 - 7, 'a', '2');
    }

    // --- Draw Figure Caption ---
    ctx.font = `bold ${fontSize - 2}px ${fontFamily}`;
    ctx.textAlign = 'center';
    ctx.fillText("Fig. 1.3", xCenter + 20, figY);

</script>
</body>
</html>